﻿using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using Sunny.UI;
using System;

namespace DevelopTool.Forms.TextProcessForms
{
    /// <summary>
    /// 根据脚本生成实体代码
    /// </summary>
    public partial class GenerateEntitiesOnScriptForm : BaseUIPage
    {
        public GenerateEntitiesOnScriptForm()
        {
            InitializeComponent();
        }

        private void GenerateEntitiesOnScriptForm_Load(object sender, EventArgs e)
        {
            this.cboScriptType.SelectedIndex = 0;
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.codeEditorIn.ContentText))
                {
                    this.ShowWarningNotifier("请先输入脚本代码");
                    return;
                }

                this.codeEditorIn.ContentFormat();

                string code = string.Empty;
                if (this.cboScriptType.SelectedText == "JSON")
                {
                    code = JsonHelper.GenerateEntitiesFromJson(this.codeEditorIn.ContentText);
                }
                else if (this.cboScriptType.SelectedText == "XML")
                {
                    code = XmlHelper.GenerateEntitiesFromXml(this.codeEditorIn.ContentText);
                }

                this.codeEditorOut.ContentText = code;
                this.codeEditorOut.ContentFormat();
                this.ShowWarningTip(this.btnGenerate, "已生成");
            }
            catch (Exception ex)
            {
                this.ShowErrorDialog(ex.Message);
            }
        }


        private void cboScriptType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cboScriptType.ContainsFocus)
                this.codeEditorIn.ContentType = this.cboScriptType.SelectedText;
        }
    }
}
